package com.ktwlsoft.archivemanage.domain.business.repositoryService;

import java.util.Date;
import java.util.List;

import com.alibaba.fastjson.JSONObject;
import com.ktwlsoft.archivemanage.domain.dao.entity.OperationLog;
import com.ktwlsoft.archivemanage.domain.dao.entity.Role;
import com.ktwlsoft.archivemanage.domain.dao.repository.IRoleMapper;
import com.ktwlsoft.archivemanage.payload.exception.BusinessException;
import com.ktwlsoft.archivemanage.payload.operationLog.SaveLogRequest;
import com.ktwlsoft.archivemanage.payload.role.AddRoleRequest;
import com.ktwlsoft.archivemanage.payload.role.EditRoleRequest;
import com.ktwlsoft.archivemanage.payload.role.QueryRoleRequest;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.validation.Valid;

/**
 * role--角色业务层接口实现类
 *
 * @author zhuhe on 2019-10-24 14:19:43
 * @version 1.0
 */
@Service
public class RoleRepositoryService {
	
	private final IRoleMapper roleMapper;

	public RoleRepositoryService(IRoleMapper roleMapper) {
		this.roleMapper = roleMapper;
	}

	/**
	 * 分页查询
	 * @param request 查询请求
	 * @return
	 */
	public PageInfo<Role> findPage(@Valid QueryRoleRequest request) {
		PageHelper.startPage(request.getPn(),request.getPageSize());
		PageInfo<Role> pageInfo = new PageInfo<Role>(roleMapper.findList(request));
		return pageInfo;
	}

    /**
	 * 查询集合 on 2019-10-24 14:19:43
	 * @param role role
	 * @return List<Role>
	 */
	public List<Role> findList(@Valid QueryRoleRequest role) {
		PageHelper.startPage(role.getPn(),role.getPageSize());
		PageInfo<Role> pageInfo = new PageInfo<Role>(roleMapper.findList(role));

		return roleMapper.findList(role);
	}

    /**
	 * 根据ID查询信息 on 2019-10-24 14:19:43
	 * @param id	主键
	 * @return Role
	 */
	public Role getById(Long id){
		return roleMapper.getById(id);
	}

   /**
	 * 新增 on 2019-10-24 14:19:43
	 * @param roleRequest role
	 * @return int
	 */
	public int insert(AddRoleRequest roleRequest){
		Role role = new Role(roleRequest.getRoleName(), roleRequest.getRemark());
		OperationRepositoryService.saveLog(new SaveLogRequest(
				OperationLog.OperationType.ADD,
				"添加角色《"+ roleRequest.getRoleName() +"》",
				"角色管理",
				OperationLog.BusinessType.SYSTEM));
		return roleMapper.insert(role);
	}

   /**
	 * 修改 on 2019-10-24 14:19:43
	 * @param role role
	 * @return int
	 */
	public int update(EditRoleRequest role){
		return roleMapper.update(role);
	}

   /**
	 * 根据id集合批量删除信息 on 2019-10-24 14:19:43
	 * @param roleArr	主键编号集合
	 * @return int
	 */
	public int deleteByIds(List<Long> roleArr){
		return roleMapper.deleteByIds(roleArr);
	}

   /**
	 * 根据id删除信息 on 2019-10-24 14:19:43
	 * @param id id
	 * @return int
	 */
   @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRES_NEW)
	public int deleteById(Long id){
		whetherDeletable(id);
		return roleMapper.deleteById(id);

	}

	private void whetherDeletable(Long id) {
		Role role = getById(id);
		if(role.getSystemDefault()){
			throw new BusinessException("默认角色不能删除");
		}
	}

	public List<Role> getByUserName(String userName) {
		return roleMapper.getByUserName(userName);
	}

	public int updateRoleRights(Long roleId, String actionName) {
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("roleId",roleId);
		jsonObject.put("actionName",actionName);
		return roleMapper.updateRoleRights(jsonObject);
	}
}
